﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of CommBodyCtrl class
	/// Control for diusplaying the currently selected comm-body
	/// parameters, plus button for opening comm-body selector
	/// </summary>
	public partial class CommBodyCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Event and Delegates

		public event EventHandler SelectionChanged;

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<CommBodySummaryTransport> m_commBodyList = null;

		private int m_iCommBodyId = 0;
		public int CommBodyId
		{
			get { return m_iCommBodyId; }
		}

		private List<string> m_strSectionList = new List<string>();
		public List<string> SectionList
		{
			get { return m_strSectionList; }
		}

		private Definitions.eCommBodyType m_commBodyType = Definitions.eCommBodyType.NotSet;
		public Definitions.eCommBodyType CommBodyType
		{
			get { return m_commBodyType; }
			set { m_commBodyType = value; }
		}

		public bool IsEmpty
		{
			get
			{
				return(m_iCommBodyId == 0);
			}
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public CommBodyCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void BrowseButton_Click(object sender, EventArgs e)
		{
#if DEBUG
			Debug.Assert(m_commBodyList != null);
			Debug.Assert(m_commBodyType != Definitions.eCommBodyType.NotSet);
#endif // #if DEBUG

			// Show the editing form
			PickCommBodyWnd wnd = new PickCommBodyWnd(
				m_commBodyList, 
				m_commBodyType, 
				m_iCommBodyId, 
				m_strSectionList
			);
			wnd.Closed += new EventHandler(wnd_Closed);
			wnd.Show();

		}

		void wnd_Closed(object sender, EventArgs e)
		{

			// Update the member variables
			PickCommBodyWnd wnd = sender as PickCommBodyWnd;
			m_iCommBodyId = wnd.GetCommBodyId();
			m_strSectionList = wnd.GetSectionList();

			// Update summary text
			_updateText();

			// Fire event
			if (SelectionChanged != null)
				SelectionChanged(this, e);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void InitializeControls(List<CommBodySummaryTransport> commBodyList)
		{
#if DEBUG
			Debug.Assert(m_commBodyList == null);
			Debug.Assert(m_commBodyType != Definitions.eCommBodyType.NotSet);
#endif // #if DEBUG

			// Initialize member variables
			m_commBodyList = commBodyList;

			// Update summary text
			_updateText();

		}

		public void Set(CommBodyWrapper commBody)
		{
#if DEBUG
			Debug.Assert(m_commBodyList != null);
			Debug.Assert(m_commBodyType != Definitions.eCommBodyType.NotSet);
#endif // #if DEBUG

			// Update member variables
			m_iCommBodyId = commBody.CommBodyId;
			m_strSectionList.Clear();
			m_strSectionList.AddRange(commBody.Sections);

			// Update summary text
			_updateText();

		}

		public void Get(CommBodyWrapper commBody)
		{
#if DEBUG
			Debug.Assert(m_commBodyList != null);
			Debug.Assert(m_commBodyType != Definitions.eCommBodyType.NotSet);
#endif // #if DEBUG

			// Update object members
			commBody.CommBodyId = m_iCommBodyId;
			commBody.Sections.Clear();
			commBody.Sections.AddRange(m_strSectionList);

		}

		private void _updateText()
		{

			// Find comm-body
			CommBodySummaryTransport commBody = null;
			if(m_iCommBodyId != 0)
				commBody = m_commBodyList.FirstOrDefault(cbt => cbt.CommBodyId == m_iCommBodyId);

			// If comm-body is set...
			if (commBody != null)
			{
				StringBuilder sb = new StringBuilder();
				sb.Append(commBody.Name);
				if (m_strSectionList.Count > 0)
				{
					sb.Append(" [");
					for (int i1 = 0; i1 < m_strSectionList.Count; i1++)
					{
						if (i1 > 0)
							sb.Append(", ");
						sb.Append(m_strSectionList[i1]);
					}
					sb.Append("]");
				}
				tbCommBody.Text = sb.ToString();
			}
			else
				tbCommBody.Text = Definitions.NotSet;

		}

	}

}
